<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 地址功能类
 *
 * @author       Eric Lee
 */
class Address
{

    private $model = null;

    public function __construct()
    {
        $this->model = M('addr_area');
    }

    /**
     * 获得某级别地址信息
     *
     * @param int $level  级别信息
     * @param int $is_gat 是否包含港澳台 0 默认包含 1 不包含
     *
     * @return mixed 地址信息数组
     */
    public function get_by_level($level, $is_gat = 0)
    {
        $m = $this->model;
        if (!is_numeric($level))
            return false;
        $condition['level'] = array('eq', $level);
        if ($is_gat) {
            $condition['city_code'] = array('NOT IN', '710000,810000,820000');
        }
        $level_add_info = $m->where($condition)->select();
        return $level_add_info;
    }

    /**
     * 获得子级别地址信息
     *
     * @param int $pid 父地址信息
     *
     * @return mixed 地址信息
     */
    public function get_by_pid($pid, $is_gat = 0)
    {
        $m = $this->model;
        if (!is_numeric($pid))
            return false;
        $condition['pid'] = array('eq', $pid);
        if ($is_gat) {
            $condition['city_code'] = array('NOT IN', '710000,810000,820000');
        }
        $level_add_info = $m->where($condition)->select();
        return $level_add_info;
    }

    /*
     * 获得地址code
     * @param string $name  地址名
     * @return int  地址编号 
     */

    public function get_code_by_name($name)
    {
        $m = $this->model;
        if (empty($name))
            return false;
        $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=2')->find();
        if (!$code)
            $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=1')->find();
        if (!$code)
            $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=0')->find();
        if ($code)
            return $code['city_code'];
        else
            return false;
    }

    /**
     * 获得地址名
     *
     * @param int $code
     *
     * @return string 地址名
     */
    public function get_name_by_code($code)
    {
        $m = $this->model;
        if (!($code))
            return false;
        $code = $m->field('city_name')->where('city_code=\'' . $code . '\'')->find();
        if ($code)
            return $code['city_name'];
        else
            return false;
    }

    /**
     * 获取省份信息
     *
     * @param int $city_code 地区编号
     */
    public function get_province($city_code)
    {
        $m = $this->model;
        return $m->alias('a')->field('a.city_name,b.city_name as province')
            ->join(C('DB_PREFIX') . "addr_area b ON a.pid=b.city_code")
            ->where("a.city_code='" . $city_code . "'")->find();
    }


    /**
     * 获取省份code
     *
     * @param $city_code
     *
     * @return mixed
     */
    public function get_province_code($city_code)
    {
        $m = $this->model;
        return $m->where(array('city_code' => $city_code))->getField('pid');
    }

    /**
     * 通过区id获取全部地址
     *
     * @param int $area_id
     *
     * @return mixed
     */
    public function get_full_name($area_id)
    {
        $map = array();
        $map['city_code'] = array('eq', $area_id);
        $res = M('addr_area')->field('full_name')->where($map)->find();
        if ($res)
            return $res['full_name'];
        else
            return false;
    }

    /**
     * 获取开展业务的城市列表
     *
     * @param array $map 检索条件
     *
     * @return mixed
     */
    public static function get_apply_city($map = array())
    {
        $arr = array();
        $rs = M('apply_city')->where($map)->select();
        foreach ($rs as $k => $v) {
            $arr[$v['code']] = $v['name'];
        }
        return $arr;
    }

    /**
     * 获取开展业务的城市列表
     *
     * @param array $map 检索条件
     *
     * @return mixed
     */
    public static function get_apply_city_with_branch($map = [])
    {
        $cols = 'ac.code,ac.name,bc.branch_id';
        $list = M('apply_city')->alias('ac')->field($cols)
            ->join(' LEFT JOIN ' . C('DB_PREFIX') . "branch_city as bc ON ac.`code` = bc.`city_code`")
            ->where($map)
            ->select();
        return $list;
    }

    /**
     * 找到分公司下对应开展业务的城市
     *
     * @param $id
     *
     * @return mixed
     */
    public function get_city_by_companyid($id)
    {
        $map = array();
        $map['bc.branch_id'] = array('eq', $id);
        return M('branch_city')->alias('bc')->field('group_concat(bc.city_code) as city_code')
            ->join(C('DB_PREFIX') . "apply_city ac ON ac.code = bc.city_code")
            ->where($map)->group('bc.branch_id')->find();
    }

    /**
     * 找到分公司下对应开展业务的城市（多维数组）
     *
     * @param $id
     *
     * @return mixed
     */
    public function getCityByBranchId($branch_id)
    {
        $map = array();
        $map['bc.branch_id'] = array('eq', $branch_id);
        return M('branch_city')->alias('bc')->field('bc.city_code,aa.city_name')
            ->join(C('DB_PREFIX') . "addr_area aa ON aa.city_code = bc.city_code")
            ->where($map)->select();
    }



    /**
     * 获取经营城市对应的分公司
     *
     * @param $city_code
     *
     * @return int
     */
    public static function get_branch_company_by_city_code($city_code)
    {
        $map = [];
        $map['city_code'] = ['eq', $city_code];
        return M('branch_city')->where($map)->getField('branch_id');
    }


    public function detail($city_code)
    {
        $m = $this->model;
        return $m->where(array('city_code' => $city_code))->find();
    }

    public function getbranchInfoByCity($city_code)
    {
        $map = array();
        $map['bc.city_code'] = array('eq', $city_code);
        return M('branch_city')->alias('bc')->field('bc.branch_id,d.name')
            ->join(C('DB_PREFIX') . "depart d ON d.id = bc.branch_id")
            ->where($map)->find();
    }

}
//end class